package krt.wid.gslzs.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import krt.wid.gslzs.R;
import krt.wid.gslzs.base.BaseActivity;
import krt.wid.gslzs.bean.MainBottomInfo;
import krt.wid.gslzs.fragment.MainFragment;
import krt.wid.gslzs.fragment.WebFragment;
import krt.wid.gslzs.util.GlideUtil;
import krt.wid.gslzs.util.UrlUtil;
import krt.wid.gslzs.view.SViewPager;

public class GovMainActivity extends BaseActivity {

    String url = "http://192.168.1.248:9002/web_ui/zyl/gsl/zf-fgzx.html";
    @BindView(R.id.viewpager)
    SViewPager mViewPager;
    @BindView(R.id.indicator)
    MagicIndicator mIndicator;
    private MyAdapter mAdapter;

    private List<MainBottomInfo> mBottomList = new ArrayList<>();

    private List<Fragment> mFragments = new ArrayList<>();

    private CommonNavigatorAdapter bottomAdapter;

    private boolean isExit;

    private String[] names = {"首页", "非公资讯", "政企互动", "提醒督办", "数据中心"};
    private int[] img = {R.mipmap.main, R.mipmap.zxzx, R.mipmap.zqhd, R.mipmap.txdb, R.mipmap.sjzx};
    private int[] selimg = {R.mipmap.selmain, R.mipmap.selzxzx, R.mipmap.selzqhd, R.mipmap.seltxdb, R.mipmap.selsjzx};
    private int[] icolor = {R.color.color_c4c4c4, R.color.color_c4c4c4, R.color.color_c4c4c4, R.color.color_c4c4c4, R.color.color_c4c4c4};
    private int[] selcolor = {R.color.color_3087f8, R.color.color_04b326, R.color.color_f12f2f, R.color.color_ffa302, R.color.color_1a1a1a};

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        for (int i = 0; i < img.length; i++) {
            MainBottomInfo minfo = new MainBottomInfo();
            minfo.setiColor(icolor[i]);
            minfo.setSelectColor(selcolor[i]);
            minfo.setImg(img[i]);
            minfo.setSeletImg(selimg[i]);
            minfo.setName(names[i]);
            mBottomList.add(minfo);
        }

        mFragments.add(new GovMainFragment());
        mFragments.add(new WebFragment().setUrl(UrlUtil.getUrl("FeiGongZiXun")));
        mFragments.add(new WebFragment().setUrl(UrlUtil.getUrl("ZhengShangHuDong")));
        mFragments.add(new WebFragment().setUrl(UrlUtil.getUrl("TiXingDuDan")));
        mFragments.add(new MainFragment());
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setOffscreenPageLimit(mBottomList.size());
        mViewPager.setAdapter(mAdapter);
        initIndicator();
    }

    @Override
    public void loadData() {

    }


    private void initIndicator() {
        mIndicator.setBackgroundColor(Color.parseColor("#fafafa"));
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(bottomAdapter = new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public IPagerTitleView getTitleView(final Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);

                // load custom layout
                View customLayout = LayoutInflater.from(context).inflate(R.layout.layout_bottom, null);
                final ImageView titleImg = (ImageView) customLayout.findViewById(R.id.img);
                final TextView titleText = (TextView) customLayout.findViewById(R.id.title);
                GlideUtil.load(GovMainActivity.this, mBottomList.get(index).getImg(), titleImg);
                titleText.setText(mBottomList.get(index).getName());
                titleText.setTextColor(ContextCompat.getColor(context, mBottomList.get(index).getiColor()));
                commonPagerTitleView.setContentView(customLayout);

                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {

                    @Override
                    public void onSelected(int index, int totalCount) {
                        if (index == 4) return;
                        GlideUtil.load(GovMainActivity.this, mBottomList.get(index).getSeletImg(), titleImg);
                        titleText.setTextColor(ContextCompat.getColor(context, mBottomList.get(index).getSelectColor()));
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        if (index == 4) return;
                        GlideUtil.load(GovMainActivity.this, mBottomList.get(index).getImg(), titleImg);
                        titleText.setTextColor(Color.LTGRAY);
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
                        if (index == 4) return;
                        titleImg.setScaleX(1.5f + (0.8f - 1.3f) * leavePercent);
                        titleImg.setScaleY(1.5f + (0.8f - 1.3f) * leavePercent);
                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
                        if (index == 4) return;
                        titleImg.setScaleX(0.7f + (1.3f - 0.9f) * enterPercent);
                        titleImg.setScaleY(0.7f + (1.3f - 0.9f) * enterPercent);
                    }
                });

                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (index == 4) return;
                        mViewPager.setCurrentItem(index);
                    }
                });

                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        mIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    private class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();
        }
        return true;
    }

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            showToast("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2 * 1000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }

}
